"""
@author: Sergiy Tsymbalyuk
"""

from sqlalchemy import Column, ForeignKey, UniqueConstraint
from sqlalchemy.types import Integer, Numeric
from pylons.i18n import lazy_ugettext as _
from pylons.i18n.translation import lazify

from pubservices.model.meta import Base
from chowa.model import District

__all__ = ['Utilities']

t = lazify(lambda x, y: u'%s (%s)' % (x, y))

def numcol(precision, scale, verbose, total):
    return Column(Numeric(precision, scale),
                  default=0,
                  info=dict(verbose=verbose,
                            total=total))

class Utilities(Base):
    __tablename__ = 'pubservices_utilities'
    __table_args__ = (UniqueConstraint('year', 'month', 'decade', 'district_id'),)
    
    id = Column(Integer, primary_key=True, info={'no-change': True})
    
    year = Column(Integer, info={'no-change': True, 'verbose': _('year')})
    month = Column(Integer, info={'no-change': True, 'verbose': _('month')})
    decade = Column(Integer, info={'no-change': True, 'verbose': _('decade')})
    district_id = Column(Integer, ForeignKey(District.id),
                         info={'no-change': True, 'verbose': _('district')})
    
    water_volume = numcol(10, 3, t(_('Volume, m3'), _('total')), 'sum')
    water_volume_tenant = numcol(10, 3, t(_('Volume, m3'), _('tenant')), 'sum')
    water_amount = numcol(10, 2, t(_('Amount, uah'), _('total')), 'sum')
    water_amount_tenant = numcol(10, 2, t(_('Amount, uah'), _('tenant')), 'sum')
    
    elec_energy = numcol(10, 3, t(_('Energy, kW'), _('total')), 'sum')
    elec_energy_tenant = numcol(10, 3, t(_('Energy, kW'), _('tenant')), 'sum')
    elec_amount = numcol(10, 2, t(_('Amount, uah'), _('total')), 'sum')
    elec_amount_tenant = numcol(10, 2, t(_('Amount, uah'), _('tenant')), 'sum')
    
    heat_energy = numcol(10, 3, t(_('Energy, Gcal'), _('total')), 'sum')
    heat_energy_tenant = numcol(10, 3, t(_('Energy, Gcal'), _('tenant')), 'sum')
    heat_amount = numcol(10, 2, t(_('Amount, uah'), _('total')), 'sum')
    heat_amount_tenant = numcol(10, 2, t(_('Amount, uah'), _('tenant')), 'sum')
    
    gas_volume = numcol(10, 3, t(_('Volume, m3'), _('total')), 'sum')
    gas_volume_tenant = numcol(10, 3, t(_('Volume, m3'), _('tenant')), 'sum')
    gas_amount = numcol(10, 2, t(_('Amount, uah'), _('total')), 'sum')
    gas_amount_tenant = numcol(10, 2, t(_('Amount, uah'), _('tenant')), 'sum')

    area = numcol(10, 2, t(_('Area, m2'), _('total')), 'max')
    area_tenant = numcol(10, 2, t(_('Area, m2'), _('tenant')), 'max')
    # TODO: calculate from hrms
    staff = Column(Integer, default=0, info=dict(verbose=_('Staff'), total='max'))
